package cn.wolfcode.controller;

import cn.wolfcode.domain.Strategy;
import cn.wolfcode.query.StrategyQuery;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.IStrategyCatalogService;
import cn.wolfcode.service.IStrategyService;
import cn.wolfcode.service.IStrategyThemeService;
import cn.wolfcode.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("strategy")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCatalogService catalogService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")StrategyQuery qo){
        Page<Strategy> page = strategyService.query(qo);

        model.addAttribute("page", page);
        return "strategy/list";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy){

        strategyService.saveOrUpdate(strategy);

//        if(strategy.getId() == null){
//            strategyService.save(strategy);
//        }else{
//            strategyService.update(strategy);
//        }
        return JsonResult.success();
    }


    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){

        strategyService.delete(id);

        return  JsonResult.success();
    }

    @RequestMapping("/input")
    public String input(Model model, String id){

        if(StringUtils.hasLength(id)){
            model.addAttribute("strategy",strategyService.get(id));
        }
        model.addAttribute("catalogs",catalogService.groupList());


        model.addAttribute("themes",strategyThemeService.findAll());
        return "/strategy/input";
    }


    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(String id, int state){

        strategyService.changeState(id, state);
        return  JsonResult.success();
    }

}
